package Classes.GameDatas
{
	import Classes.GameUtils.GU_dealPower;
	import Classes.GameUtils.GU_dealSharePreWin;
	import Classes.GameViews.GV_Map;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MMT_Character;
	import Classes.GameViews.ViewPanels.VP_Main;
	import Classes.GameViews.ViewPanels.VP_OpenWinView;
	import Classes.Managers.M_ConfigData;
	import Classes.staticinfo.Language;
	import Classes.staticinfo.PlatformInfo;
	
	import UIComponents.FlatInfo;
	
	import com.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.system.ApplicationDomain;
	import flash.utils.Timer;

	public class GD_AllFriends extends GameData
	{
		private static var _instance:GD_AllFriends; /*静态引用*/
		
		/**
		 * 好友列表对象 
		 */
		private var _AllFriendsArr:Array=new Array();
		
		/**
		 * 被雇佣的好友列表 (原始表)
		 */		
		private var _allHiredFriends:Object=new Object();
		
		/**
		 *   被雇佣的好友列表 (存储GD_hiredFriend,如果对象中途走了 是不会在此表删掉的)
		 */		
		public var hiredFirendObj:Object=new Object();
		
		public function GD_AllFriends()
		{
		}
		
		/*getter & setter*/
		public static function get instance():GD_AllFriends
		{
			if (!_instance)
				_instance=new GD_AllFriends;
			return _instance;
		}
	//**************************被雇佣好友对象 ********************************	
		/**
		 * 创建被雇佣的好友对象 
		 * @param arr
		 * 
		 */	
		/**
		 * 临时变量   
		 */		
		private var tmpHiredFriendArr:Array;
		public function createHiredFirends(arr:Array):void
		{
			this.tmpHiredFriendArr=arr;
			if(this.tmpHiredFriendArr.length>0)
			{
				this.createHireFriend(tmpHiredFriendArr[0]);
			}
		}
		
		private function callCreateFiredFriends(id:Number=0):void
		{
			this.tmpHiredFriendArr.splice(0,1);
			if(this.tmpHiredFriendArr.length>0)
			{
				this.createHireFriend(tmpHiredFriendArr[0]);
			}
		}
		
		
		private function createHireFriend(obj:Object):void
		{
			var gd:GD_HiredFriend;
			if(String(obj.pf)!=PlatformInfo.instance.pf)
			{
				gd=new GD_HiredFriend(false);
				gd.playerID=obj.zt_user_id+0.11;
			}else{
				gd=new GD_HiredFriend(true);
				gd.playerID=obj.zt_user_id;
			}
			gd.pf=obj.pf;
			
			gd.character=GD_Character.creat({job:obj.zt_role_id, life:obj.zt_hire_blood});
			
			if(String(obj.pf)!=PlatformInfo.instance.pf)
			{
				gd.character.controlEnabled=false;
			}else{
				gd.character.controlEnabled=true;
			}
			gd.character.characterState=1;
			gd.actPoint=obj.zt_hire_power;
			gd.character.characterId=gd.playerID;
			if(null==obj.zt_user_nick_name)
			{
				gd.playerName="";
			}else{
				gd.playerName=obj.zt_user_nick_name;
			}
			
			if(String(obj.pf)!=PlatformInfo.instance.pf)
			{
				var tmpName:String=Language[obj.pf]==null?Language.msr:Language[obj.pf];
				gd.playerName=tmpName;
			}
			
			gd.playerViewID=obj.zt_user_pic;
			gd.playerJob=obj.zt_role_id;
			gd.playerSex=obj.zt_sex;
			gd.playerLv=obj.zt_exp_id;
			gd.weaponId=obj.zt_weapon_id;
			gd.startHiredTime=Number(obj.zt_hire_time)/1000;
			gd.character.gd_characterPlay=gd;
			this.allHiredFriends[gd.playerID]=obj;
			this.hiredFirendObj[gd.playerID]=gd;
			
			//如果不在平台则不加入地图
//			if(String(obj.pf)!=PlatformInfo.instance.pf)
//			{
//				callCreateFiredFriends(gd.playerID);
//			}else{
			gd.needCartoon(callCreateFiredFriends);
			if(String(obj.pf)==PlatformInfo.instance.pf)
			{
				this.getFriendInfoById(gd.playerID).isHired	=1;
			}
			//new guide
			if(gd.playerID==1)
			{
				GU_dealPower.instance.clickFriendBtn();
			}
//			}
			
		}
		
		/**
		 * 保存雇佣好友的一部分信息(血量、行动力) 
		 * @param life
		 * @param act
		 */		
		public function saveHiredFriendPartInfo(firndId:Number,life:int,act:int):void
		{
			if(null==this.allHiredFriends[firndId])return;
			var hiredObj:Object=this.allHiredFriends[firndId];
			hiredObj.zt_hire_power=act;
			hiredObj.zt_hire_blood=life;
		}
		
		
		/**
		 * 回家创建自己的雇佣的好友 
		 */		
	/*	private var tmpArr:Array;
		public function goHomeCreateHiredFriend():void
		{
			tmpArr=[];
			for each(var gd:GD_HiredFriend in this.allHiredFriends)
			{
				tmpArr.push(gd);
			}
			if(tmpArr.length>0)
			{
				(this.tmpArr[0] as GD_HiredFriend).needCartoon(goHomeAddFiredInMap);
			}
		}*/
		/**
		 * 递归 把好友加入地图中 
		 */		
	/*	private function goHomeAddFiredInMap(id:Number=0):void
		{
			this.tmpArr.splice(0,1);
			if(tmpArr.length>0)
			{
				(this.tmpArr[0] as GD_HiredFriend).needCartoon(goHomeAddFiredInMap);
			}
		}*/
		
		
		/**
		 * 时间转化 把  
		 * @param time
		 * @return 
		 * 
		 */		
		public function changeTimeToMis(time:String):Number
		{
			var t:Array=time.split("-");
			var data:Date=new Date(t[0],(t[1]-1),t[2],t[3],t[4],t[5]);
			return (data.getTime()/1000);
		}
		
		public var systemFriendId:Number;
		/**
		 * 创建好友列表对象  
		 * @param friendArr
		 */		
		public function createFriend(friendArr:Array):void
		{
			this.AllFriendsArr=[];
			//放入自己到好友列表中
			var my:GD_SingleFriend=new GD_SingleFriend();
			my.friendViewID=GD_Player.instance.playerViewID;
			my.friendID=GD_Player.instance.playerID;
			my.friendJob=GD_Player.instance.playerJob.toString();
			my.friendLV=GD_Player.instance.playerLv;
			my.friendName=GD_Player.instance.playerName;
			my.playerSex=GD_Player.instance.playerSex;
			my.friendQqVip=GD_Player.instance.qqVipId;
			my.friendWeaponId=int(GD_Player.instance.currentWeapon);
			my.isMySelft=true;
			this.AllFriendsArr.push(my);
			
			this.systemFriendId=1;
			FlatInfo.sysFriendId=this.systemFriendId;
			//放入好友
			for each(var obj:Object in friendArr)
			{
				var _friendId:Number=obj.friendID;
				var friend:GD_SingleFriend=new GD_SingleFriend();
				friend.friendViewID=obj.friendViewID;
				friend.friendID=_friendId;
				friend.friendJob=obj.friendJob;
				friend.friendLV=obj.friendLV;
				friend.friendAct=obj.remainActPoint;
				friend.friendQqVip=int(obj.friendQqVip);
				if(obj.friendName)
				{
					friend.friendName=obj.friendName;
				}else{
					friend.friendName="";
				}
				friend.isHired=obj.isHired;
				friend.visitSign=obj.visitSign;
				friend.friendWeaponId=obj.zt_weapon_id;
				friend.isMySelft=false;
				friend.friendHiredMoney=obj.friendHiredMoney;
				friend.playerSex=obj.friendSex;
				if(String(obj.pf)!=PlatformInfo.instance.pf)
				{
					if(friend.visitSign==1)M_ConfigData.instance.maxVisitleNum--;
					continue;
				}
				if(_friendId==1)
				{
					this.AllFriendsArr.splice(1,0,friend);
				}else{
					this.AllFriendsArr.push(friend);
				}
			}
			
			VP_Main.instance.setMaxVisableFriendNum(M_ConfigData.instance.maxVisitleNum);
		}
		
		/**
		 * 访问好友的地图数据对象 
		 */			
		private var _mapData:GD_Map;
		/**
		 * 当前访问的好友 
		 */		
		public var currentVisableFriend:GD_SingleFriend;
		/**
		 * 当前访问好友Id 
		 */		
		public var currentVisableFriendId:Number=0;
		/**
		 * 当前访问好友所携带的武器 
		 */		
		private var currentVisableWeaponId:Number;
		
		
		/**
		 * 去好友家  初始化好友信息
		 * @param obj
		 */	
		public function initVisiableFriend(dataObj:Object):void
		{
			//我的建筑对象  支持我在好友家时  任务拥有建筑的功能
			if(GD_Player.instance.character.characterState==0)
			{
				GD_Player.instance.myBuildObj=GV_Map.instance.mapData.buildingsObj;
			}
			//清空地图上面的对象                
			GV_Map.instance.release_GVMap();
			GV_Map.instance.reset();


			//设置我的友情行动力
			GD_Player.instance.friendAct=dataObj.baseData.power;
			//设置被访问好友的性别
			this.currentVisableFriend.playerSex=dataObj.baseData.playerSex;
			this.currentVisableWeaponId=dataObj.baseData.weaponID;
			//初始化地图上面的 杂物 僵尸 建筑等
			this._mapData=GD_Map.creat(dataObj.wallsAry, dataObj.buildingsAry, dataObj.zombiesAry, dataObj.sickMansAry, dataObj.sundriesAry);
			this._mapData.creatMap();
			this.createFriendOfFriend(dataObj.hiredAry);
			
			getFriendInfoById(currentVisableFriendId).visitSign	=	1;
			getFriendInfoById(currentVisableFriendId).friendAct	=	GD_Player.instance.friendAct;
	
		}
		/**
		 * 包含了被访问朋友雇佣的好友  跟朋友自己 
		 */		
		private var tmpFriendOfFriendArr:Array; 
		/**
		 * 创建 被访问好友的  雇用好友
		 * @param arr
		 */		
		public function createFriendOfFriend(arr:Array):void
		{
			this.tmpFriendOfFriendArr=[];
			this.tmpFriendOfFriendArr=arr;
			
			//被访问的好友角色数据
			var tmpObj:Object=new Object();
			tmpObj.zt_user_id=this.currentVisableFriendId;
			tmpObj.zt_role_id=this.currentVisableFriend.friendJob;
			tmpObj.zt_hire_blood=5;
			var tmpName:String=this.currentVisableFriend.friendName.length>7?this.currentVisableFriend.friendName.substr(0,8)+"...":this.currentVisableFriend.friendName;
			tmpObj.zt_user_nick_name=tmpName+Language.zhuren;
			tmpObj.zt_weapon_id=this.currentVisableWeaponId;
			tmpObj.zt_sex=this.currentVisableFriend.playerSex;
			tmpObj.zt_user_pic="";
			tmpObj.zt_exp_id=1;
			this.tmpFriendOfFriendArr.push(tmpObj);
			
			this.createVisitCharater(tmpFriendOfFriendArr[0]);
		}
		
		private var listerTimer:Timer;
		
		private function callCreateVisitCharater(id:Number=0):void
		{
			this.tmpFriendOfFriendArr.splice(0,1);
			if(this.tmpFriendOfFriendArr.length>0)
			{
				this.createVisitCharater(tmpFriendOfFriendArr[0]);
			}else{
				var tmpState:uint=GD_Player.instance.character.characterState;
				GD_Player.instance.character.characterState=2;
				if(tmpState==0)
				{
					GD_Player.instance.touchObserve("isInHome");
				}
				GV_Map.instance.creatCharacter(GD_Player.instance.character, true);
				
				listerTimer=new Timer(1000,3);
				listerTimer.addEventListener(TimerEvent.TIMER_COMPLETE,completeTimerHalder);
				listerTimer.start();
			}
		}
		
		private function completeTimerHalder(evt:TimerEvent):void
		{
			listerTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,completeTimerHalder);
			listerTimer=null;
			//以后可以在此处加入移除 播放动画的代码
			GV_Map.instance.addListener();

//			this.stopSmallLoading();			//移除 小加载条
			VP_OpenWinView.instance.closeWin();
			
			//newGuide
			GU_dealPower.instance.isInFriendHome=true;
			GU_dealPower.instance.isSelectFriend=false;
			GU_dealPower.instance.removeArrowForFriend();
			GU_dealPower.instance.clickGoHome();
			
			VP_Main.instance.interfaceUI["visibleFriendId"]	=	currentVisableFriendId;
			
			GD_Player.instance.tweenCenter();
		}
		
		private function createVisitCharater(obj:Object):void
		{
			var gd:GD_HiredFriend=new GD_HiredFriend(false);
			gd.playerID=obj.zt_user_id;
			gd.character=GD_Character.creat({job:obj.zt_role_id, life:obj.zt_hire_blood});
			gd.character.controlEnabled=false;
			gd.character.characterState=10;
			gd.actPoint=obj.zt_hire_power;
			gd.character.characterId=gd.playerID;
			gd.playerName=obj.zt_user_nick_name==null?"":obj.zt_user_nick_name;
			gd.playerViewID=obj.zt_user_pic;
			gd.playerJob=obj.zt_role_id;
			gd.playerSex=obj.zt_sex;
			gd.playerLv=obj.zt_exp_id;
			gd.weaponId=obj.zt_weapon_id;
			gd.character.gd_characterPlay=gd; 
			gd.needCartoon(callCreateVisitCharater);
		}
		

		/**
		 * 好友列表对象 
		 */
		public function get AllFriendsArr():Array
		{
			return _AllFriendsArr;
		}

		public function set AllFriendsArr(value:Array):void
		{
			_AllFriendsArr = value;
		}
		
		/**
		 * 获取好友等级>=自己等级  的 %
		 * @param lv
		 * @return 
		 * 
		 */		
		public function getFriendNumByLv(lv:Number=0.5):int
		{
			var f:int=-1;
			var myLv:int=Math.floor(GD_Player.instance.playerLv*lv);
			for each(var friend:GD_SingleFriend in this.AllFriendsArr)
			{
				if(friend.friendLV>=myLv)
				{
					f++;
				}
			}
			return f;
		}
		
		/**
		 * 根据好友Id 获取好友信息从好友列表中 
		 * @param frendId
		 * @return 
		 */		
		public function getFriendInfoById(frendId:Number):GD_SingleFriend
		{
			var gdFriend:GD_SingleFriend;
			for each(var friend:GD_SingleFriend in this.AllFriendsArr)
			{
				if(friend.friendID==frendId)
				{
					gdFriend=friend;
					break;
				}
			}
			return gdFriend;
		}

		/**
		 * 存储被雇佣的好友对象  里面是一个原始的Object对象
		 */
		public function get allHiredFriends():Object
		{
			return _allHiredFriends;
		}
		
		/**
		 * 获取雇佣好友数目 
		 * @return 
		 */		
		public function getHiredNum():int
		{
			var i:int=0;
			for each(var obj:Object in allHiredFriends)
			{
				i++;
			}
			return i;
		}
		
		/**
		 * 获取雇佣好友数组 
		 * @return 
		 */		
		public function getHiredFriendArr():Array
		{
			var arr:Array=new Array();
			for each(var obj:Object in allHiredFriends)
			{
				arr.push(obj);
			}
			return arr;
		}

		/**
		 * @private
		 */
		public function set allHiredFriends(value:Object):void
		{
			_allHiredFriends = value;
		}

		
		//****************回放好友***************************
		private var _playerBackFriendArr:Array=new Array();/*回放好友对象*/
		
		
		public function creatPlayerBackData(obj:Object):void
		{
			if(null!=this.getPlayerFriend(obj.zt_friend_id))
			{
				obj.gdFriend=this.getPlayerFriend(obj.zt_friend_id).gdFriend;
				this._playerBackFriendArr.push(obj);
				return;
			}
			var tmpFriend:GD_SingleFriend=this.getFriendInfoById(obj.zt_friend_id);
			var isNullFrind:Boolean=false;
			if(null==tmpFriend)
			{
				isNullFrind=true;
				var findex:int=int(Math.random()+this.AllFriendsArr.length);

			//	trace("模拟下标为="+findex);
				
				//即没有一个好友
				if(AllFriendsArr.length<2)
				{
					return;
				}
				if(findex==0)
				{
					findex=1;
				}
				
				tmpFriend=AllFriendsArr[findex];
			}
			
			var gd:GD_HiredFriend=new GD_HiredFriend(false);
			gd.playerID=obj.zt_friend_id;
			gd.character=GD_Character.creat({job:tmpFriend.friendJob, life:5});
			gd.character.controlEnabled=false;
			gd.character.characterState=3;
			gd.actPoint=5;
			gd.character.characterId=gd.playerID;
			gd.playerName=tmpFriend.friendName==null?"":tmpFriend.friendName;
			if(isNullFrind)gd.playerName=Language.friendName;
			gd.playerViewID=tmpFriend.friendViewImg;
			gd.playerJob=Number(tmpFriend.friendJob);
			gd.playerSex=tmpFriend.playerSex;
			gd.playerLv=tmpFriend.friendLV;
			gd.weaponId=tmpFriend.friendWeaponId;
			gd.character.gd_characterPlay=gd; 
			obj.gdFriend=gd;
			this._playerBackFriendArr.push(obj);
			gd.needCartoon(playFriendOperate);
		}
		
		/**
		 * 根据Id来获取回放的好友 以及所在数组的位置
		 * @param friendId
		 * @return 
		 */		
		private function getPlayerFriend(friendId:Number):Object
		{
			var len:Number=this.playerBackFriendArr.length;
			var tmpObj:Object=null;
			for(var i:int=0;i<len;i++)
			{
				if(friendId==this.playerBackFriendArr[i].zt_friend_id)
				{
					tmpObj=this.playerBackFriendArr[i];
					tmpObj.index=i;
					break;
				}
			}
			return tmpObj;
		}
		
		/**
		 * 回放好友在我家的操作 
		 */		
		private function playFriendOperate(friendId:Number):void
		{
			var obj:Object=this.getPlayerFriend(friendId);
			var _gdCharater:GD_Character=((obj.gdFriend) as GD_HiredFriend).character;
			_gdCharater.view.speed=GD_Player.instance.speed;
			switch(obj.zt_commond_id)
			{
				case 128000://打僵尸
					new CharaterTips(_gdCharater,Language.fActZombel,false,1,callZombieFun);
					break;
				case 128001: //修围墙
					new CharaterTips(_gdCharater,Language.fReparidWall,false,1,callWallFun);
					break;
				case 128002://清理杂物
					new CharaterTips(_gdCharater,Language.fSundry,false,1,callSundryFun);
					break;
				case 127002://通知她雇佣了我
					this.playFriendOver(_gdCharater);
					break;
			}
		}
		
		public function callZombieFun(charaterView:MMT_Character):void
		{
			var gdjs:GD_Zombie=GV_Map.instance.getRamdonZombie();
			if(null==gdjs){
				this.playFriendOver(charaterView.characterData,true);
			}else{
				charaterView.setActTarget(gdjs.view);
			}
		}
		
		private function callWallFun(charaterView:MMT_Character):void
		{
			charaterView.setActTarget(GV_Map.instance.getRamdonWall().view);
		}
		
		public function callSundryFun(charaterView:MMT_Character):void
		{
			var gdzw:GD_Building_Sundry=GV_Map.instance.getRamdonSundry();
			if(null==gdzw){
				this.playFriendOver(charaterView.characterData,true);
			}else{
				charaterView.setActTarget(gdzw.view);
			}
		}
		
		public function playFriendOver(gd:GD_Character,nothingTodo:Boolean=false):void
		{
			var gd_charater:GD_Character=gd as GD_Character;
			var friendObj:Object=this.getPlayerFriend(gd_charater.characterId);
			GD_Player.instance.creatDropRewards(friendObj);
			this.playerBackFriendArr.splice(friendObj.index,1);
			//判断此角色还是否有其他的操作
			var obj:Object=this.getPlayerFriend(gd_charater.characterId);
			if(null!=obj)
			{
				this.playFriendOperate(gd_charater.characterId);
			}else{
				var tmpStr:String=Language.doSomethingOver;
				if(nothingTodo)
				{
					tmpStr=Language.noSomethiing;
				}
				var tips:String=friendObj.zt_commond_id==127002?Language.hiredMoeny:tmpStr;
				new CharaterTips(gd_charater,tips);
				
			}
		}

		/**
		 * 回放好友对象 
		 */
		public function get playerBackFriendArr():Array
		{
			return _playerBackFriendArr;
		}
	
	}
}


import Classes.GameDatas.GD_Character;
import Classes.GameViews.GV_Map;
import Classes.GameViews.ViewPanels.VP_BubbleUp;

import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.utils.Timer;

class CharaterTips extends Sprite
{
	private var timer:Timer;
	private var jsq:int=0;
	private var _character:GD_Character;
	private var vp_bubbleUp:VP_BubbleUp;
	private var isRemoveCharater:Boolean;
	private var popTimer:uint;
	private var callBackFun:Function;

	/**
	 * 泡泡类 
	 * @param gd 角色数据对象
	 * @param tips 冒泡类容
	 * @param _isRemoveCharater 是否冒泡玩之后移除角色对象  默认为是
	 * @param popTimer 隔多久冒泡
	 */	
	public function CharaterTips(gd:GD_Character,tips:String="",_isRemoveCharater:Boolean=true,_popTimer:uint=3,_callBack:Function=null):void
	{
		//人物隔5秒冒出泡泡 在消失
		this._character=gd;
		this.isRemoveCharater=_isRemoveCharater;
		this.popTimer=_popTimer;
		this.callBackFun=_callBack;
		timer=new Timer(1000,5);
		timer.addEventListener(TimerEvent.TIMER,timerHalder);
		timer.addEventListener(TimerEvent.TIMER_COMPLETE,completeTimerHalder);
		timer.start();
		
		vp_bubbleUp=new VP_BubbleUp();
		this.vp_bubbleUp.setPopTxts(tips);
		GV_Map.instance.popTipsLayer.addChild(this.vp_bubbleUp);
		vp_bubbleUp.visible=false;
	}
	
	private function timerHalder(evt:TimerEvent):void
	{
		jsq++;
		if(jsq>=this.popTimer)
		{
			this.vp_bubbleUp.x=this._character.view.x;
			this.vp_bubbleUp.y=this._character.view.y-this._character.view.height+20;
			this.vp_bubbleUp.visible=true;
		}
	}
	
	private function completeTimerHalder(evt:TimerEvent):void
	{
		timer.removeEventListener(TimerEvent.TIMER,timerHalder);
		timer.removeEventListener(TimerEvent.TIMER_COMPLETE,completeTimerHalder);
		timer.stop();
		timer=null;
		this.jsq=0;
		if(isRemoveCharater)
		{
//			GV_Map.instance.removeChildInMap=_character.characterId;
			//this._character.view.removeSign=1;
			this._character.release();
			_character=null;
		}
		GV_Map.instance.popTipsLayer.removeChild(this.vp_bubbleUp);
		this.vp_bubbleUp=null;
		if(null!=this.callBackFun && null!=_character)
		{
			this.callBackFun(this._character.view);
		}
		this.callBackFun=null;
	}
}